<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <template v-if="$store.getters.isLogin">
          <li>
            <a href="javascript:;"
              ><i class="iconfont icon-user"></i
              >{{ $store.state.user.userInfo.nickname }}</a
            >
          </li>

          <li>
            <a href="javascript:;" @click="exit">退出登录 </a>
          </li>
        </template>
        <template v-else>
          <li><a href="javascript:;" @click="login">请先登录</a></li>
          <li><a href="javascript:;">免费注册</a></li>
        </template>
        <li>
          <a href="javascript:;">我的订单</a>
        </li>
        <li><a href="javascript:;">会员中心</a></li>
        <li><a href="javascript:;">帮助中心</a></li>
        <li><a href="javascript:;">关于我们</a></li>
        <li>
          <a href="javascript:;"><i class="iconfont icon-phone"></i>手机版</a>
        </li>
      </ul>
    </div>
  </nav>
</template>
<script>
import Confirm from '@/components/library/Confirm'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
export default {
  name: 'AppTopnav',
  setup () {
    const store = useStore()
    const route = useRoute()
    const router = useRouter()
    function exit () {
      /*
  1:清除用户信息
  2：清除token
  3:附上用户误操作：使用确定弹框组件

*/
      Confirm({ title: '温馨提示', text: '你确定退出该网站吗?' })
        .then(() => {
          store.commit('user/logout')
        })
        .catch(() => {})
    }
    //  登陆
    function login () {
      router.push('/login?redirect=' + encodeURIComponent(route.fullPath))
    }

    return { exit, login }
  }
}
</script>
<style scoped lang="less">
.app-topnav {
  background: #333;
  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;
    li {
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: @xtxColor;
        }
      }
      ~ li {
        a {
          border-left: 2px solid #666;
        }
      }
    }
  }
}
</style>
